<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$error = FALSE;
$route_id = $_GET['id'];
$route = $routeMgr->get($route_id);
if(!$route) {
    header('Location: /notfound.php');
    exit();
}

if(isset($_SESSION['msg']) && $_SESSION['msg']) {
   $error = $_SESSION['msg'];
   unset($_SESSION['msg']);
}

$route_info = $routeMgr->getStats($route_id);

$latest_comments = $routeMgr->getLatestComments($route_id);

$latest_comments_info = array('<ul>');
foreach($latest_comments as $row) {
    switch($row['completed']) {
        case -1:
            $completed = '<span class="notcompleted">&nbsp;</span>';
            break;
        case 0:
            $completed = '<span class="completed">&nbsp;</span>';
            break;
        default:
            $text = $langMgr->get('text_completed_with_x_blocks', FALSE, array('num' => $row['completed']));
            $completed = '<span class="blockcompleted" title="' . $text . '">&nbsp;</span>';
            break;
    }
    $latest_comments_info[] = '<li>[' . date('d-m-Y', strtotime($row['timestamp'])) . '] <b><a href="/viewuser.php?id=' . $row['user_id'] . '">' . $row['name'] . '</a></b>: ' . $completed . '<div class="comment">' . $row['comment'] . '</div></li>';
}
$latest_comments_info[] = '</ul>';

$replaces = array(
    'colour'                    => $route['colour'],
    'type'                      => ($route['type_name'] ? $route['type_name'] : $langMgr->get('text_empty_value')),
    'route_type'                => ($route['unicode'] ? '<img class="routetype ' . $route['route_type'] . '" src="/img/pixel.png">' : ''),
    'difficulty'                => $route['difficulty'],
    'route_name'                => $route['name'],
    'rope'                      => ($route['rope'] ? $route['rope'] : $langMgr->get('text_empty_value')),
    'is_short'                  => ($route['is_short'] ? $langMgr->get('text_route_short') : $langMgr->get('text_route_long')),
    'author_info'               => ($route['author_id'] ? '<a href="/viewauthor.php?id=' . $route['author_id'] . '">' : '') . ($route['author'] ? $route['author'] : $langMgr->get('text_empty_value')) . '</a>',
    'created'                   => ($route['created'] ? date('d-m-Y', strtotime($route['created'])) : $langMgr->get('text_empty_value')),
    'location_id'               => $route['location_id'],
    'location'                  => $route['location'],
    //'climbed_count'             => $route_info['count'],
    //'climbed_usercount'         => $route_info['usercount'],
    //'climbed_completed'         => $route_info['completed'],
    //'climbed_completed_perc'    => ($route_info['completed'] ? round(100 * $route_info['completed'] / $route_info['count']) . '%' : 'never'),
    'latest_comments_info'      => (count($latest_comments_info) > 2 ? implode("\n", $latest_comments_info) : $langMgr->get('text_route_nocomments')),
    'removed'                   => ($route['removed'] ? '<li>' . $langMgr->get('text_route_removed') . '</li>' : ''),
    'checkin'                   => (!$route['removed'] ? '<li><a href="/checkin.php?route_id=' . $route_id . '">' . $langMgr->get('text_route_checkin') . '</a></li>' : ''),
    'admin_info'                => (in_array($_SESSION['user']['id'], $admin_users) ? '<li><a href="/editroute.php?id=' . $route_id . '">' . $langMgr->get('text_edit_route') . '</a></li>' : ''),
    'error'                     => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_routeinfo'              => $langMgr->get('header_routeinfo'),
    'text_locatedat'                => $langMgr->get('text_locatedat'),
    'text_colour'                   => $langMgr->get('text_colour'),
    'text_difficulty'               => $langMgr->get('text_difficulty'),
    'text_routetype'                => $langMgr->get('text_routetype'),
    'text_rope'                     => $langMgr->get('text_rope'),
    'text_createdby'                => $langMgr->get('text_createdby'),
    'text_on'                       => $langMgr->get('text_on'),
    'header_statistics'             => $langMgr->get('header_statistics'),
    'text_route_stats'              => $langMgr->get('text_route_stats', false, array('climbed_count' => $route_info['count'], 'climbed_usercount' => $route_info['usercount'])),
    'text_route_completion_stats'   => $langMgr->get('text_route_completion_stats', false, array('climbed_completed' => $route_info['completed'], 'climbed_completed_perc' => ($route_info['completed'] ? round(100 * $route_info['completed'] / $route_info['count']) . '%' : $langMgr->get('text_never')))),
    'text_latest_comments'          => $langMgr->get('text_latest_comments'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
